package cn.player.utils;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;


/**
 * Fragment操作 工具类
 * @author zengliang
 *
 */
public class FragUtils {
	
	/**
	 * 获取FragmentTransaction,可设置动画等相关属性, 配合hide、add等操作，并在最后調用commit()方法
	 * @param fragment
	 * @return
	 */
	public static FragmentTransaction getFragTran(Fragment fragment){
		return fragment.getChildFragmentManager().beginTransaction();  
	}
	
	/**
	 * 获取FragmentTransaction,可设置动画等相关属性, 配合hide、add等操作，并在最后調用commit()方法
	 * @param activity 
	 * @return
	 */
	public static FragmentTransaction getFragTran(FragmentActivity activity){
		return activity.getSupportFragmentManager().beginTransaction();  
	}
	
	/**
	 * 隐藏Fragment，不会调用fragment生命周期方法，但会调用onHiddenChanged()方法
	 * @param activity
	 * @param fragment
	 */
	public static FragmentTransaction hide(FragmentTransaction mFragTransaction,Fragment fragment){
		if(fragment !=null && fragment.isAdded() && !fragment.isHidden()){
//			FragmentTransaction mFragTransaction = activity.getSupportFragmentManager().beginTransaction();  
			mFragTransaction.hide(fragment);
		}
		return mFragTransaction;
	}
	
	/**
	 * 显示Fragment，不会调用fragment生命周期方法，但会调用onHiddenChanged()方法
	 * @param activity
	 * @param fragment
	 */
	public static FragmentTransaction show(FragmentTransaction mFragTransaction,Fragment fragment){
		if(fragment !=null && fragment.isAdded() && fragment.isHidden()){
			mFragTransaction.show(fragment);
		}
		return mFragTransaction;
	}
	
	/**
	 * 将Fragment  detach出去，fragment生命周期调用onPause()----->.....----->onDestroyView()
	 * @param activity
	 * @param fragment
	 */
	public static FragmentTransaction detach(FragmentTransaction mFragTransaction,Fragment fragment){
		if(fragment !=null && fragment.isAdded() && !fragment.isDetached()){
//			FragmentTransaction mFragTransaction = activity.getSupportFragmentManager().beginTransaction();  
			mFragTransaction.detach(fragment);
		}
		return mFragTransaction;
	}
	/**
	 * 将Fragment  attach进来，fragment生命周期调用onCreateView()----->.....----->onResume()
	 * @param activity
	 * @param fragment
	 */
	public static FragmentTransaction attach(FragmentTransaction mFragTransaction,Fragment fragment){
		if(fragment !=null && fragment.isDetached()){
//			FragmentTransaction mFragTransaction = activity.getSupportFragmentManager().beginTransaction();  
			mFragTransaction.attach(fragment);
		}
		return mFragTransaction;
	}
	
	/**
	 * 将Fragment  remove掉，fragment生命周期调用onPause()----->.....----->onDetach()
	 * @param activity
	 * @param fragment
	 */
	public static FragmentTransaction remove(FragmentTransaction mFragTransaction,Fragment fragment){
		if(fragment !=null && fragment.isAdded()){
//			FragmentTransaction mFragTransaction = activity.getSupportFragmentManager().beginTransaction();  
			mFragTransaction.remove(fragment);
		}
		return mFragTransaction;
	}
	
	/**
	 * 将Fragment  add进来，fragment生命周期调用onAttach()----->.....----->onResume()
	 * @param activity
	 * @param containerViewId
	 * @param fragment
	 */
	public static FragmentTransaction add(FragmentTransaction mFragTransaction,int containerViewId, Fragment fragment){
		if(fragment !=null){
//			FragmentTransaction mFragTransaction = activity.getSupportFragmentManager().beginTransaction();  
			if(fragment.isAdded()){
				mFragTransaction.remove(fragment);
			}
			mFragTransaction.add(containerViewId,fragment);
		}
		return mFragTransaction;
	}
	
	/**
	 * 将Fragment  add进来，fragment生命周期调用onAttach()----->.....----->onResume()
	 * @param activity
	 * @param fragment
	 * @param tag
	 */
	public static FragmentTransaction add(FragmentTransaction mFragTransaction, Fragment fragment, String tag){
		if(fragment !=null){
//			FragmentTransaction mFragTransaction = activity.getSupportFragmentManager().beginTransaction();  
			if(fragment.isAdded()){
				mFragTransaction.remove(fragment);
			}
			mFragTransaction.add(fragment,tag);
		}
		return mFragTransaction;
	}
	/**
	 * 将Fragment  add进来，fragment生命周期调用onAttach()----->.....----->onResume()
	 * @param activity
	 * @param containerViewId
	 * @param fragment
	 * @param tag
	 */
	public static FragmentTransaction add(FragmentTransaction mFragTransaction,int containerViewId, Fragment fragment, String tag){
		if(fragment !=null){
//			FragmentTransaction mFragTransaction = activity.getSupportFragmentManager().beginTransaction();  
			if(fragment.isAdded()){
				mFragTransaction.remove(fragment);
			}
			mFragTransaction.add(containerViewId,fragment, tag);
		}
		return mFragTransaction;
	}

}
